<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">模板名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="模板名称不能为空" placeholder="请填写模板名称" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">模板编号</label>
        <div class="layui-input-block">
            <input type="text" name="templateId" lay-verify="required" lay-reqtext="模板Code不能为空" placeholder="请填写模板名称"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">模板类型</label>
        <div class="layui-input-block">
            <input type="text" name="type" lay-verify="required" lay-reqtext="模板类型不能为空" placeholder="请填写模板名称" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">签名</label>
        <div class="layui-input-block">
            <select name="signName" lay-filter="signNameFilter" lay-reqtext="签名不能为空">
                <option value="">请选择签名</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">模板内容</label>
        <div class="layui-input-block">
            <textarea name="content" placeholder="请输入内容" lay-verify="required" rows="8" lay-reqtext="模板内容不能为空"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入内容" rows="3" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../js/lay-module/xm-select-master/dist/xm-select.js" charset="utf-8"></script>
<script>

    window.onload = function () {
        // 初始化签名列表
        initSignList();
    }

    function initSignList() {
        $.ajax({
            type: "GET",
            url: '/sms/sign/list?status=' + 0,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            success: function (req) {
                if (req.code === '200') {
                    let signList = req.data
                    if (signList.length > 0) {
                        layui.use(['layer', 'form'], function () {
                            var form = layui.form,
                                $ = layui.$;
                            let options = "";
                            for (const index in signList) {
                                options += "<option value='" + signList[index].signName + "'>" + signList[index].signName + "</option>";
                            }
                            $("select[name='signName']").append(options);// 重新渲染下拉框
                            form.render('select');
                            // 初始化
                            init(id);
                        });
                    } else {
                        layer.msg('请先录入签名');
                        setTimeout(function () {
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    }
                } else {
                    layer.msg(req.msg);
                    setTimeout(function () {
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }, 1500);
                }
            },
            error: function (e) {
                layer.msg("数据异常");
                setTimeout(function () {
                    let iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                }, 1500);
            }
        })
    }

    layui.use(['layer', 'form', 'layarea'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定提交吗？", {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...", {
                    icon: 16,
                    time: -1
                })
                $.ajax({
                    type: "post",
                    url: '/sms/template/save',
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json; charset=UTF-8',
                    success: function (req) {
                        if (req.code === '200') {
                            layer.msg("已添加");
                            layer.close(index);
                            // 关闭弹出层
                            setTimeout(function () {
                                layer.close(myMsg);//手动关闭
                                let iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            }, 1500);
                        } else {
                            layer.msg(req.msg);
                        }

                    },
                    error: function (e) {
                        layer.msg("添加失败");
                        layer.close(myMsg);//手动关闭
                    }
                })

            });

            return false;
        });

    });
</script>
</body>
</html>
